//
//  MainViewController.swift
//  weile
//
//  Created by LKM on 16/10/8.
//  Copyright © 2016年 LKM. All rights reserved.
//

import UIKit

class MainTabViewController: UITabBarController {//,UITabBarDelegate{

    override func viewDidLoad() {
        super.viewDidLoad()
        //底部栏的 图标颜色
        tabBar.tintColor = R.colors.base_color;//UIColor(red: 245 / 255, green: 80 / 255, blue: 83 / 255, alpha: 1.0)
//        tabBar.delegate = self;
        // 添加子控制器
        addChildViewControllers()
    }
    override func viewDidLayoutSubviews() {
        MainTabBarHeight = tabBar.height
    }
    /**
     # 添加子控制器
     */
    fileprivate func addChildViewControllers() {
        addChildViewController(ActivityRequest.getHomeViewController(), title: "主页", imageName: "ic_main_bar_1")
        addChildViewController(ActivityRequest.getArticlesViewController(), title: "资讯", imageName: "ic_main_bar_2")
        addChildViewController(ActivityRequest.getSuppliersViewController(), title: "提供商", imageName: "ic_main_bar_3")
        addChildViewController(ActivityRequest.getMyCenterViewController(), title: "个人中心", imageName: "ic_main_bar_4")
    }
    /**
     # 初始化子控制器
     
     - parameter childControllerName: 需要初始化的控制器
     - parameter title:               标题
     - parameter imageName:           图片名称
     */
    fileprivate func addChildViewController(_ vc:UIViewController ,title: String, imageName: String) {
        /**
         //动态获取命名空间
        let ns = Bundle.main.infoDictionary!["CFBundleExecutable"] as! String
        // 将字符串转化为类，默认情况下命名空间就是项目名称，但是命名空间可以修改
        let cls: AnyClass? = NSClassFromString(ns + "." + childControllerName)
        let vcClass = cls as! UIViewController.Type
        let vc = vcClass.init()
        */
        //let vc = HomeViewController()
        // 设置对应的数据
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.accessibilityIdentifier = String(CollectionHelp.size(childViewControllers))//vc.classForCoder;
        //vc.tabBarItem.selectedImage = UIImage(named: imageName + "_p")
        vc.title = title
        // 给每个控制器包装一个导航控制器
        let nav = BaseNavigationController()
        nav.addChildViewController(vc)
        addChildViewController(nav)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
//        super.tabBar(tabBar, didSelect: item) //这句会 引发unrecognized selector sent to instance 0x10142f6d0'
        //其实现在页面还没跳转到点击的那个item的
        if(item.accessibilityIdentifier == "0"){
            ActivityRequest.goMain()
        }else if(item.accessibilityIdentifier == "3"){
            if(UserInfoCache.getInstance().checkGuest()){
                //其实现在页面还没跳转到点击的那个item的。所以得延迟一下再跳，否则就还是刚点击那个，因为他慢改变
                Handler.post {
                    self.selectedIndex = 0;
                }
            }
            
        }
        
        
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
